﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProviderQuestions : System.Web.UI.Page
{
    UserPhone phone
    {
        get
        {
            if (Session["SESS_USER_PHONE"] == null)
            {
                Session["SESS_USER_PHONE"] = new UserPhone(UUID);
            }
            return Session["SESS_USER_PHONE"] as UserPhone;
        }
        set
        {
            Session["SESS_USER_PHONE"] = value;
        }
    }

    string UUID
    {
        get
        {
            string uuid = "";
            if (Request.Cookies["UUID"] != null)
            {
                uuid = Request.Cookies["UUID"].Value;
            }
            return uuid;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (phone.Scores != null)
            {
                calculateScore();
            }
        }
        catch
        {
        }
    }

    protected void calculateScore()
    {
        double tAis = 1.0;
        double tDtac = 1.0;
        double tTmove = 1.0;

        phone.Scores = new string[grdQuestion.Rows.Count];
        int i = 0;

        foreach (GridViewRow grow in grdQuestion.Rows)
        {
            TableCell cell = grow.Cells[2];
            double ais = double.Parse(((Label)cell.FindControl("lblAis")).Text);
            double dtac = double.Parse(((Label)cell.FindControl("lblDtac")).Text);
            double tmove = double.Parse(((Label)cell.FindControl("lblTmove")).Text);
            string rawScore = ((TextBox)cell.FindControl("txtScore")).Text;
            double score = double.Parse(rawScore) / 100;

            tAis = tAis * Math.Pow(ais, score);
            tDtac = tDtac * Math.Pow(dtac, score);
            tTmove = tTmove * Math.Pow(tmove, score);

            phone.Scores[i++] = rawScore;
        }


        const int REL_MAX = 400;
        double max = Math.Max(Math.Max(tAis, tDtac), tTmove);
        double vAis = Math.Round(tAis * REL_MAX / max, 4);
        double vDtac = Math.Round(tDtac * REL_MAX / max, 4);
        double vTmove = Math.Round(tTmove * REL_MAX / max, 4);

        //Session["SESS_AIS"] = tAis *100 / max;
        //Session["SESS_DTAC"] = tDtac * 100 / max;
        //Session["SESS_TMOVE"] = tTmove * 100 / max;


        phone.AisScore = tAis * 100 / max;
        phone.DtacScore = tDtac * 100 / max;
        phone.TmoveScore = tTmove * 100 / max;

        double sum = vAis + vDtac + vTmove;


        lblAis.Text = Math.Round(vAis / sum, 4)*100 + " % ";
        lblDtac.Text = Math.Round(vDtac / sum, 4) * 100 + " % ";
        lblTmove.Text = Math.Round(vTmove / sum, 4) * 100 + " % ";

        imgRateAis.Width = new Unit(vAis, UnitType.Pixel);
        imgRateDtac.Width = new Unit(vDtac, UnitType.Pixel);
        imgRateTmove.Width = new Unit(vTmove, UnitType.Pixel);

        PhoneLoader.SerializeToXML(phone);
        //PhoneLoader.SerializeToXML(phone);
    }

    protected void btnResult_Click(object sender, EventArgs e)
    {
        try
        {
            calculateScore();
        }
        catch
        {
        }
    }
    protected void grdQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        TableCell cell0 = e.Row.Cells[0];
        TableCell cell2 = e.Row.Cells[2];
        if (cell2.FindControl("txtScore") != null)
        {
            int i = int.Parse( cell0.Text) -1;

            if ((phone.Scores != null ) && (phone.Scores.Length > i))
            {
                ((TextBox)cell2.FindControl("txtScore")).Text = phone.Scores[i];
            }
        }
    }
}